﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PPOrderTest.aspx.cs" Inherits="UI_Orders_PPOrderTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>PayPal订单管理</title>
    <script src="../../js/common.js" type="text/javascript"></script>
    <link href="../../css/admin.css" rel="stylesheet" type="text/css" />
    <link href="../../js/jquery.easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../js/jquery.easyui/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../js/jquery.easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../../js/FileUpload/ajaxfileupload.js" type="text/javascript"></script>
    <style type="text/css">
        #huserMsg div{color:Blue; font-weight:bold;}
    </style>
<script type="text/javascript">
    var _interval;//定时
    $(function () {

        $("#ddlebayUserId").combobox({
            editable: false
        });
        var defaultDate = Date.parse('1900/01/01');

        var _$paypalorder = $("#paypalorder");
        _$paypalorder.datagrid({
            border: true,
            height: 345,
            title: 'PayPal订单列表...',
            remoteSort: false,
            nowrap: true,
            striped: false,
            //url: "../../TableHanders.aspx?act=q&tid=42&type=0",
            sortName: 'TransactionId',
            sortOrder: 'desc',
            loadMsg: "正在加载,请稍后...",

            frozenColumns: [[
	            { title: 'EbayUserId', field: 'EbayUserId', width: 120, sortable: true },
	            { title: 'TransactionId', field: 'TransactionId', width: 130, sortable: true, formatter: function (val, rec) {
	                if (rec.StatusId == '2') {
	                    return '<span style="color:red;">' + val + '</span>';
	                }
	                else {
	                    return val;
	                }
	            }
	            }
				]],
            columns: [[
	                { title: '类别', field: 'CategoryName', width: 80, sortable: true },
	                { title: 'DateTime', field: 'CreateDateTime', width: 130, sortable: true },
	                { title: 'TimeZone', field: 'TimeZone', width: 80, sortable: true },
	                { title: 'Name', field: 'Name', width: 150, sortable: true },
	                { title: 'Type', field: 'Type', width: 200, sortable: true },
	                { title: 'Status', field: 'Status', width: 120, sortable: true },
	                { title: 'Subject', field: 'Subject', width: 80, sortable: true },
	                { title: 'Currency', field: 'Currency', width: 80, sortable: true },
	                { title: 'Gross', field: 'Gross', width: 80, sortable: true, align: 'right' },
	                { title: 'Fee', field: 'Fee', width: 80, sortable: true, align: 'right' },
	                { title: 'Net', field: 'Net', width: 80, sortable: true, align: 'right' },
	                { title: 'Note', field: 'Note', width: 80, sortable: true },
	                { title: 'FromEmailAddress', field: 'FromEmailAddress', width: 200, sortable: true },
	                { title: 'ToEmailAddress', field: 'ToEmailAddress', width: 200, sortable: true },
	                { title: 'PaymentType', field: 'PaymentType', width: 80, sortable: true },
	                { title: 'CounterpartyStatus', field: 'CounterpartyStatus', width: 120, sortable: true },
	                { title: 'ShippingAddress', field: 'ShippingAddress', width: 120, sortable: true },
	                { title: 'AddressStatus', field: 'AddressStatus', width: 120, sortable: true },
					{ field: 'PostageandPackingAmount', title: 'PostageandPackingAmount', width: 160, sortable: true, align: 'right' },
					{ field: 'InsuranceAmount', title: 'InsuranceAmount', width: 120, sortable: true, align: 'right' },
					{ field: 'VAT', title: 'VAT', width: 85, sortable: true, align: 'right' },
					{ field: 'AuctionSite', title: 'AuctionSite', width: 85, sortable: true },
					{ field: 'BuyerID', title: 'BuyerID', width: 130, sortable: true },
					{ field: 'ClosingDate', title: 'ClosingDate', width: 130, sortable: true, formatter: function (val) {
					    var d = Date.parse(val.replace("-", "/"));
					    if (d == defaultDate) {
					        return "";
					    }
					    else {
					        return val;
					    }
					}
					},
					{ field: 'ReferenceTxnID', title: 'ReferenceTxnID', width: 130, sortable: true },
					{ field: 'InvoiceNumber', title: 'InvoiceNumber', width: 125, sortable: true },
					{ field: 'SubscriptionNumber', title: 'SubscriptionNumber', width: 140, sortable: true },
					{ field: 'CustomNumber', title: 'CustomNumber', width: 100, sortable: true },
					{ field: 'ReceiptID', title: 'ReceiptID', width: 85, sortable: true },
					{ field: 'Balance', title: 'Balance', width: 85, sortable: true, align: 'right' },
					{ field: 'AddressLine1', title: 'AddressLine1', width: 200, sortable: true },
					{ field: 'AddressLine2', title: 'AddressLine2', width: 200, sortable: true },
					{ field: 'City', title: 'City', width: 85, sortable: true },
					{ field: 'County', title: 'County', width: 150, sortable: true },
					{ field: 'PostalCode', title: 'PostalCode', width: 85, sortable: true },
					{ field: 'Country', title: 'Country', width: 130, sortable: true },
					{ field: 'ContactPhoneNumber', title: 'ContactPhoneNumber', width: 140, sortable: true },
					{ field: 'BalanceImpact', title: 'BalanceImpact', width: 140, sortable: true },
					{ field: 'ToInvoice', title: '已发货', width: 80, sortable: true, formatter: function (val, rec) {
					    if (val == 'Y') {
					        return '是';
					    }
					    else {
					        return '否';
					    }
					}
					},
					{ field: 'ToPz', title: '写入凭证', width: 80, sortable: true, formatter: function (val, rec) {
					    if (val == 'Y') {
					        return '是';
					    }
					    else {
					        return '否';
					    }
					}
					},
					{ field: 'StatusId', title: '状态', width: 50, sortable: true, formatter: function (val, rec) {
					    if (val == '2') {
					        return '<span style="color:red;">已匹配</span>';
					    }
					    else {
					        return '未匹配';
					    }
					}
					}
				]],
            pagination: true,
            rownumbers: true,
            singleSelect: true,
            pageList: [10, 15, 20],
            queryParams: {
                "timeFrom": $("#beginDate").datebox('getValue'),
                "timeTo": $("#endDate").datebox('getValue'),
                "EbayUserId": $("#ddlebayUserId").combobox('getValue'),
                "transId": $("#txtTransId").val()
            }
        });
        $("#btnQuery").click(function () {

            if (!$("#p").form('validate'))
                return;
            var timeFrom = $("#beginDate").datebox('getValue');
            var timeTo = $("#endDate").datebox('getValue');
            var ebayUserId = $("#ddlebayUserId").combobox('getValue');
            var transId = $("#txtTransId").val();
            _$paypalorder.datagrid({
                url: "../../TableHanders.aspx?act=q&tid=42&type=0",
                queryParams: {
                    "timeFrom": timeFrom,
                    "timeTo": timeTo,
                    "ebayUserId": ebayUserId,
                    "transId": transId
                }
            });
        });
        $('#beginDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });
        $('#endDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });
//        $('#txtMatchDate').datebox({ formatter: function (date) {
//            var y = date.getFullYear();
//            var m = date.getMonth() + 1;
//            var d = date.getDate();
//            return y + "-" + m + "-" + d;
//        }
//        });
        $('#txtMatchBeginDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });
        $('#txtMatchEndDate').datebox({ formatter: function (date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            return y + "-" + m + "-" + d;
        }
        });
        function ajaxFileUploads() {
            WaitBox.Show();
            $.ajaxFileUpload(
                   {
                       url: '../../UpLoadFile.ashx?type=excel&tid=42&id=' + new Date().getTime(),
                       secureuri: false,
                       fileElementId: 'fup',
                       dataType: 'text',
                       success: function (data, status) {
                           WaitBox.Close();
                           var retData = EvalData(data);
                           if (retData.retCode = 0) {
                               alert('成功导入!');
                           }
                           else {
                               alert(retData.retMsg);
                           }
                       },
                       error: function (data, status, e) {
                           WaitBox.Close();
                           alert("更新失败!");
                       }
                   });
        }

        $("#btnImPort").click(function () {
            var file = $("#fup");
            var filePath = getPath(file);
            if (filePath == "") {
                alert("请选择一个excel文件");
                return;
            }
            var fileRegex = /.*[.]csv$/;
            if (fileRegex.exec(filePath) == null) {
                alert("文件名应为.csv结尾!");
                return;
            }
            ajaxFileUploads();
        });
        $("#btnMatch").click(function () {
            $("#huserMsg").text("请确认数据都已导入在开始匹配");
            $('#divmatch').window('open');
        });
        $("#btnthok").click(function () {
//            var matchDate = $("#txtMatchDate").datebox('getValue');
//            if (matchDate == "") {
//                alert('匹配日期不能为空!');
//                return;
//            }
            var matchbeginDate = $("#txtMatchBeginDate").datebox('getValue');
            if (matchbeginDate == "") {
                alert('匹配日期不能为空!');
                return;
            }
            var matchendDate = $("#txtMatchEndDate").datebox('getValue');
            if (matchendDate == "") {
                alert('匹配日期不能为空!');
                return;
            }
            var val="";
            var matchAccountId = $("#ddlMatchEbayUserId").combobox('getValue');
            $('#btnthok').linkbutton('disable');
            $("#huserMsg").text("开始匹配,整个过程可能需要几分钟的时间,请不要关闭页面...");
            //var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=matchorderTest&type=0&matchdate=" + matchDate + "&matchAcc=" + matchAccountId + "&ts=" + new Date().getTime();  
            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=matchorderTest&type=0&matchbegindate=" + matchbeginDate + "&matchenddate=" + matchendDate +"&matchAcc=" + matchAccountId + "&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {                  
                    clearInterval(_interval); //结束定时。                   
                    $("#huserMsg").html(retData.retMsg);
                }
                else if (code == 2) { //返回当前匹配进度信息。  
                    val = retData.retMsg;
                    if (val == "start") {
                        _interval = setInterval(Match, 10000); //启动定时器
                        $('#btnthok').linkbutton('disable');                      
                    }
                    $("#huserMsg").html(retData.retMsg);
                }
                else {
                    $("#huserMsg").text(retData.retMsg);
                }
            });
        });
        $("#btnthclose").click(function () {
            $('#divmatch').window('close');
        });
        $('#divmatch').window({
            width: 500,
            //height: 230,
            height: 300,
            modal: true,
            shadow: false,
            closed: false,
            closed: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            closable: false
        });

        //检查匹配
        $("#btnCheckMatch").click(function () {
            //            $("#huserMsgVoucher").text("请确认是否开始检查");
            //            $('#divchkMatch').window('open');
            $("#huserMsgVoucher").text("开始检查匹配,整个过程可能需要几分钟的时间,请不要关闭页面...");
            WaitBox.Show();
            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=CheckMatch&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {
                WaitBox.Close();
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {
                    alert("检查成功");
                }
                else {
                    alert("检查失败");
                }
            });
        });
        //        $("#btnCHKOK").click(function () {
        //            $("#huserMsgVoucher").text("开始检查匹配,整个过程可能需要几分钟的时间,请不要关闭页面...");
        //            $('#btnCHKOK').linkbutton('disable');
        //            WaitBox.Show();
        //            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=CheckMatch&ts=" + new Date().getTime();
        //            $.getJSON(url, function (data) {
        //                WaitBox.Close();
        //                var retData = EvalData(data);
        //                var code = retData.retCode;
        //                if (code == 0) {
        //                    $("#huserMsgVoucher").text("检查完成...");
        //                }
        //                else {
        //                    $("#huserMsgVoucher").text(retData.retMsg);
        //                }
        //                $('#btnCHKOK').linkbutton('enable');
        //            });
        //        });
        //        $("#btnPCls").click(function () {
        //            $('#divchkMatch').window('close');
        //        });

        $("#btnVoucher").click(function () {
//            $("#huserMsgVoucher").text("请确认数据都已导入在开始写入凭证");
//            $('#divVoucher').window('open');
        });
        $("#btnPOK").click(function () {
            var PDate = $("#txtPDate").datebox('getValue');
            if (PDate == "") {
                alert('Paypal日期不能为空!');
                return;
            }
            $('#btnPOK').linkbutton('disable');
            $("#huserMsgVoucher").text("开始写入凭证,整个过程可能需要几分钟的时间,请不要关闭页面...");

            WaitBox.Show();
            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=writeVoucher&Pdate=" + PDate + "&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {
                WaitBox.Close();
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {
                    $("#huserMsgVoucher").text("写入凭证完成...");
                }
                else {
                    $("#huserMsgVoucher").text(retData.retMsg);
                }
                $('#btnPOK').linkbutton('enable');
            });
        });
        $("#btnPCls").click(function () {
            $('#divVoucher').window('close');
        });
        $('#divVoucher').window({
            width: 400,
            height: 230,
            modal: true,
            shadow: false,
            closed: false,
            closed: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            closable: false
        });
        //操作：

        $("#btnReplaceAccount").click(function () {
            $("#huserMsgVoucher").text("请确认数据都已导入");
            $('#divReplace').window('open');
        });
        $("#btnROK").click(function () {
            var PDate = $("#txtReplaceDate").datebox('getValue');
            if (PDate == "") {
                alert('Paypal日期不能为空!');
                return;
            }
            $('#btnROK').linkbutton('disable');

            var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=replaceaccount&Pdate=" + PDate + "&ts=" + new Date().getTime();
            $.getJSON(url, function (data) {                
                var retData = EvalData(data);
                var code = retData.retCode;
                if (code == 0) {
                    alert('替换成功!');
                }
                else {
                    alert(retData.retMsg);
                }
                $('#btnROK').linkbutton('enable');
            });
        });

        $("#btnRClose").click(function () {
            $('#divReplace').window('close');
        });
        $('#divReplace').window({
            width: 400,
            height: 230,
            modal: true,
            shadow: false,
            closed: false,
            closed: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            closable: false
        });
    });
    function Match()//定时刷新
    {
//        var matchDate = $("#txtMatchDate").datebox('getValue');
//        if (matchDate == "") {
//            alert('匹配日期不能为空!');
//            return;
//        }
        var matchbeginDate = $("#txtMatchBeginDate").datebox('getValue');
        if (matchbeginDate == "") {
            alert('匹配开始日期不能为空!');
            return;
        }
        var matchendDate = $("#txtMatchEndDate").datebox('getValue');
        if (matchendDate == "") {
            alert('匹配结束日期不能为空!');
            return;
        }     
        var matchAccountId = $("#ddlMatchEbayUserId").combobox('getValue');
       // $('#btnthok').linkbutton('disable');
       // $("#huserMsg").text("开始匹配,整个过程可能需要几分钟的时间,请不要关闭页面...");
       // var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=matchorderTest&type=1&matchdate=" + matchDate + "&matchAcc=" + matchAccountId + "&ts=" + new Date().getTime();
        var url = getRootPath() + "/UI/Orders/OrdersHandler.ashx?cmd=matchorderTest&type=1&matchbegindate=" + matchbeginDate +"&matchenddate=" + matchendDate +"&matchAcc=" + matchAccountId + "&ts=" + new Date().getTime();
        $.getJSON(url, function (data) {
            var retData = EvalData(data);
            var code = retData.retCode;
            if (code == 0) {
                $("#huserMsg").html(retData.retMsg);
                $('#btnthok').linkbutton('enable');
                clearInterval(_interval); //结束定时。
                alert("进行的匹配操作已完成!..");
            }
            else if (code == 2) { //返回当前匹配进度信息。
                $("#huserMsg").html(retData.retMsg);
            }
            else {
                $("#huserMsg").text(retData.retMsg);
            }
        });
    }
</script>
</head>
<body>
    <form id= "fomrquery" runat="server" style=" width :1100px;">
    <div class="div_Title">
        <span class="Body_Title">》》<%=this.Title %></span>
    </div>    
    <div id="p" class="easyui-panel" title="请输入查询条件..." style="margin-bottom:5px">
        <table>
            <tr>
               <td>交易号:</td>
                <td>
            	    <input id="txtTransId" runat="server" style="width:150px;"/>
                </td>
                <td>ebay帐号:</td>
                <td>
            	    <select id="ddlebayUserId" runat="server" class="easyui-combobox" style="width:180px;"></select>
                </td>
                <td>导入日期:</td>
                <td>
                    <input id="beginDate" type="text" class ="easyui-datebox" runat="server"  required="true" readonly="readonly" style="width:90px"/>
                </td>
                <td> - </td>
                <td>
                    <input id="endDate" type="text" class ="easyui-datebox" runat="server"  required="true" readonly="readonly" style="width:90px"/>
                </td>         
                <td>
                    <a id="btnQuery" href="#" class="easyui-linkbutton" icon="icon-search">Query</a>
                </td>
            </tr>
        </table>
    </div>
    
    <table id="paypalorder"></table>
    <div style="margin-top:5px">
        <div id="p1" class="easyui-panel" title="数据导入...">
            <table>
                <tr>
                    <td>选择文件:</td>
                    <td>
            	        <input type="file" id="fup" name="fup" style="width:500px"/>
                    </td>
                    <td>
                        <a id="btnImPort" class = "easyui-linkbutton"  icon="icon-reload">导入</a>
                        <a id="btnMatch" class = "easyui-linkbutton" >开始匹配</a>
                        <a id="btnVoucher" class = "easyui-linkbutton" disabled="true" >开始写入凭证</a>    
                        <a id="btnCheckMatch" class = "easyui-linkbutton" >匹配检查</a>                   
                     </td>
                     <td>
                       <%-- <a id="btnReplaceAccount" class = "easyui-linkbutton" >替换PayPal帐号</a>--%>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="divmatch" title = "请确认">
        <div style="text-align:center;">
            <div style = "padding:20px;">
                <span>匹配帐号：</span> <select id = "ddlMatchEbayUserId" runat = "server" style = "width:196px;"  class="easyui-combobox"></select>
                <br />
                <br />                
                <%--<span>匹配日期：</span><input id="txtMatchDate" type="text" class ="easyui-datebox" runat="server" style="width:200px"/>--%>
                <span>开始日期：</span><input id="txtMatchBeginDate" type="text" class ="easyui-datebox" runat="server" style="width:200px"/>
                <br />
                <span>结束日期：</span><input id="txtMatchEndDate" type="text" class ="easyui-datebox" runat="server" style="width:200px"/>
            </div>
            <div id="huserMsg" style = "padding-left:20px; text-align:left;">
            </div>
        </div>
        <div style="text-align:right; padding:10px;">
			<a id="btnthok" class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)" >Ok</a>
			<a id="btnthclose" class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)">Close</a>
		</div>
    </div>
    <div id="divVoucher" title = "请确认">
        <div style="height:150px; text-align:center;">
            <div style = "padding:40px;">
                <span>Paypal日期：</span><input id="txtPDate" type="text" class ="easyui-datebox" runat="server" style="width:100px"/>
            </div>
            <div>
                <span id="huserMsgVoucher"></span>
            </div>
        </div>
        <div style="text-align:right; padding:10px;">
			<a id="btnPOK" class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)" >Ok</a>
			<a id="btnPCls" class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)">Close</a>
		</div>
    </div>   
    <div id="divReplace" title = "请确认">
        <div style="height:150px; text-align:center;">
            <div style = "padding:40px;">
                <span>Paypal日期：</span><input id="txtReplaceDate" type="text" class ="easyui-datebox" runat="server" style="width:100px"/>
            </div>            
        </div>
        <div style="text-align:right; padding:10px;">
			<a id="btnROK" class="easyui-linkbutton" iconCls="icon-ok" href="javascript:void(0)" >Ok</a>
			<a id="btnRClose" class="easyui-linkbutton" iconCls="icon-cancel" href="javascript:void(0)">Close</a>
		</div>
    </div>   
    </form>
</body>
</html>
